﻿using OWE.Core.Common.Response;
using OWE.Core.Data;
using OWE.Core.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OWE.Service.Accounting
{
    public class AccountService : IAccountService
    {
        private readonly IRepository<Account> _accountRepository;
        private readonly IRepository<AccountMove> _accountMoveRepository;
        private readonly IRepository<AccountType> _accountTypeRepository;

        public AccountService(IRepository<Account> accountRepository, IRepository<AccountMove> accountMoveRepository, IRepository<AccountType> accountTypeRepository)
        {
            _accountRepository = accountRepository;
            _accountMoveRepository = accountMoveRepository;
            _accountTypeRepository = accountTypeRepository;
        }

        public PrimitiveResponse InsertAccount(Account account)
        {
            _accountRepository.Insert(account);
            return new PrimitiveResponse {ResponseCode = ResponseCodes.Successful};            
        }

        public PrimitiveResponse UpdateAccount(Account account)
        {
            _accountRepository.Update(account);
            return new PrimitiveResponse {ResponseCode = ResponseCodes.Successful};
        }

        public PrimitiveResponse DeleteAccount(Account account)
        {
            _accountRepository.Update(account);
            return new PrimitiveResponse { ResponseCode = ResponseCodes.Successful };
        }

        public EntityResponse<Account> GetAccountById(int id)
        {
            var account = _accountRepository.GetById(id);
            var response = new EntityResponse<Account> {EntityData = account};
            return response;
        }

        public EntityListResponse<Account> GetAllAccounts()
        {
            var response = new EntityListResponse<Account>();
            return response;
        }



        public PrimitiveResponse InsertAccountType(AccountType accountType)
        {
            _accountTypeRepository.Insert(accountType);
            return new PrimitiveResponse { ResponseCode = ResponseCodes.Successful };
        }

        public PrimitiveResponse UpdateAccountType(AccountType accountType)
        {
            _accountTypeRepository.Update(accountType);
            return new PrimitiveResponse { ResponseCode = ResponseCodes.Successful };
        }

        public PrimitiveResponse DeleteAccountType(AccountType accountType)
        {
            _accountTypeRepository.Update(accountType);
            return new PrimitiveResponse { ResponseCode = ResponseCodes.Successful };
        }

        public EntityResponse<AccountType> GetAccountTypeById(int id)
        {
            var accountType = _accountTypeRepository.GetById(id);
            var response = new EntityResponse<AccountType> { EntityData = accountType };
            return response;
        }

        public EntityListResponse<AccountType> GetAllAccountTypes()
        {
            var response = new EntityListResponse<AccountType>();
            response.EntityDataList = _accountTypeRepository.GetList().ToList();
            response.ResponseCode = ResponseCodes.Successful;
            return response;
        }




        public EntityListResponse<AccountMove> GetAllAccountsWithDebitCredits()
        {
            var response = new EntityListResponse<AccountMove>();
            response.EntityDataList = _accountMoveRepository.ExecWithStoreProcedure("sel_account_move").ToList();
            response.ResponseCode = ResponseCodes.Successful;
            return response;
        }
    }
}
